package com.erp.programmer.controller;


import com.erp.programmer.bean.CodeMsg;
import com.erp.programmer.dao.SellCompanyMapper;
import com.erp.programmer.domain.SellCompany;
import com.erp.programmer.dto.*;
import com.erp.programmer.service.IPurchaseOrderService;
import com.erp.programmer.utils.CommonUtil;
import com.erp.programmer.utils.ValidateEntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 陈鑫
 * @since 2021-05-24
 */
@RestController
@RequestMapping("/purchase_order")
@CrossOrigin
public class PurchaseOrderController {
    @Autowired
    IPurchaseOrderService orderService;

    @Autowired
    private SellCompanyMapper sellCompanyMapper;

    /**
     * 查询所有订单 或根据ID查询
     * @return
     */
    @PostMapping("/get_data")
    public ResponseDto<PageDto<PurchaseOrderDto>> getPurchaseOrder(@RequestBody PageDto<PurchaseOrderDto> pageDto) {
        if (pageDto==null) {
            ResponseDto.errorByMsg(CodeMsg.DATA_ERROR);
        }
        return orderService.getPurchaseOrder(pageDto);
    }

    /**
     * 添加采购单
     * @param purchaseOrderDto
     * @return
     */
    @PostMapping("/add")
    public ResponseDto<PageDto<PurchaseOrderDto>> insertPurchaseOrder(@RequestBody PurchaseOrderDto purchaseOrderDto) {
        if (purchaseOrderDto==null) {
            return ResponseDto.errorByMsg(CodeMsg.DATA_ERROR);
        }
        //进行统一表单验证
        CodeMsg validate = ValidateEntityUtil.validate(purchaseOrderDto);
        if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())){
            return ResponseDto.errorByMsg(validate);
        }
        SellCompany sellCompany = sellCompanyMapper.selectById(purchaseOrderDto.getSellerId());
        if(sellCompany == null){
            return ResponseDto.errorByMsg(CodeMsg.SELLER_COMPANY_NOT_EXIST);
        }
        for (PurchaseOrderDetailDto purchaseOrderDetailDto:purchaseOrderDto.getPurchaseOrderDetailDtoList()) {
            validate = ValidateEntityUtil.validate(purchaseOrderDetailDto);
            if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())){
                return ResponseDto.errorByMsg(validate);
            }
            if (CommonUtil.isEmpty(purchaseOrderDetailDto.getNumber()) || !CommonUtil.isNumber(purchaseOrderDetailDto.getNumber())) {
                return ResponseDto.errorByMsg(CodeMsg.PRODUCT_NUM_ERROR);
            }
            BigDecimal bigDecimal = new BigDecimal(purchaseOrderDetailDto.getNumber());
            if (bigDecimal.compareTo(new BigDecimal(99999999.99)) == 1 || bigDecimal.compareTo(new BigDecimal(0)) == -1) {
                return ResponseDto.errorByMsg(CodeMsg.PRODUCT_NUM_ERROR);
            }
        }
        return orderService.insertPurchaseOrder(purchaseOrderDto);
    }


    /**
     * 修改支付状态
     * @param editStateDto
     * @return
     */
    @PostMapping("/edit_state")
    public ResponseDto<PageDto<PurchaseOrderDto>> editState(@RequestBody EditStateDto editStateDto){
        return orderService.updatePaymentState(editStateDto.getId(), Integer.valueOf(editStateDto.getState()));
    }
}

